<?php
/**
 * 示例model（标注表的作用）
 * panglishan
 * 2020.08.13
 */
namespace App\Models\MainDB;

use Illuminate\Database\Eloquent\Model;

class UserExp extends Model{

    protected $table = 'ln_user_exp';     //数据库内完整表名

    protected $primaryKey = 'id';    //主键名

    protected $fillable = [
        'id',      //主键ID
        'user_id',         //用户id
        'gold',    //金币
        'money',    //金额
        'exp',    //经验值
        'ratio',    //加速百分比
        'type',    //1送礼物
        'action_id',    //关联id
        'add_time',    //添加时间
    ];

    public $timestamps = false;

    public function getUserExp($user_id) {
        $redis_key = 'u:exp:' . $user_id;
        if (app('redis')->exists($redis_key)) {
            $exp = app('redis')->get($redis_key);
            return round($exp, 2);
        }
        $exp = $this->where('user_id', $user_id)->sum('exp');
        if($exp > 0) {
            app('redis')->setex($redis_key, 604800, $exp);
            return round($exp, 2);
        }
        return 0;
    }
}